<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/10
 * Time: 16:39
 */

namespace App\HttpController\V1;

use App\Common\Checking;
class MyClassMate extends Auth
{
    /**
     * 同学数量
     * @return bool
     */
    function classMateCount(){
        $data=$this->db
            ->where('pid',$this->user->id)
            ->whereIn('lay',[1,2])
            ->count('users_position');
        return $this->writeJson(200,$data,'成功');

    }

    /**
     * 我的同学
     */
    function myClassMate(){

        $page=$this->input('page') ?: 1;
        $type=$this->input('type');//1 同学  2感召  3激发
        $date=$this->input('date');// 2019-03 //如果为空表示查询这个月
        $page_size=10;
        if(!in_array($type,[1,2,3])){
            return $this->writeJson( -201, null, '参数错误' );
        }
        switch ($type){
            case 1:
                $sql='users_position.lay=1 and users.level=1';
                break;
            case 2:
                $sql='users_position.lay=1 and users.level!=1';
                break;
            case 3:
                $sql='users_position.lay=2 and users.level!=1';
                break;
        }

        if($date){
            $dateStart=$date.'-01 00:00:00';
            $dateEnd=date('Y-m-d H:i:s',strtotime("{$dateStart} +1 month -1 second"));
            $data=$this->db
                ->where('users_position.pid',$this->user->id)
                ->where($sql)
                ->whereBetween('users.createtime',[$dateStart,$dateEnd])
                ->join('users',"users.id=users_position.user_id",'left')
                ->get('users_position',[($page-1)*$page_size,$page_size],'users.headimg,users.nickname,users.mobile,users.createtime');
        }else{
            $dateStart=date('Y-m').'-01 00:00:00';
            $dateEnd=date('Y-m-d H:i:s',strtotime("{$dateStart} +1 month -1 second"));
            $data=$this->db
                ->where('users_position.pid',$this->user->id)
                ->where($sql)
                ->whereBetween('users.createtime',[$dateStart,$dateEnd])
                ->join('users',"users.id=users_position.user_id",'left')
                ->get('users_position',[($page-1)*$page_size,$page_size],'users.headimg,users.nickname,users.mobile,users.createtime');
        }
        if($data){
            foreach ($data as $k=>&$v){
                $v['headimg'] = QINIU_URL.$v['headimg'];
                $v['mobile'] = Checking::replaceStar($v['mobile']);
            }
        }
        return $this->writeJson(200,$data,'成功');
    }

    /**
     * 感召列表
     */
    function impel(){
        $page=$this->input('page') ?: 1;
        $page_size=10;
        $data=$this->db
            ->where('users_position.pid',$this->user->id)
            ->where('users_position.lay',1)
            ->where('users.level!=1')
            ->join('users',"users.id=users_position.user_id",'left')
            ->get('users_position',[($page-1)*$page_size,$page_size],'users.headimg,users.nickname,users.mobile,users.createtime');
        if($data){
            foreach ($data as $k=>&$v){
                $v['headimg'] = QINIU_URL.$v['headimg'];
                $v['mobile'] = Checking::replaceStar($v['mobile']);
            }
        }
        return $this->writeJson(200,$data,'成功');
    }

    /**
     * 激发列表
     * @return bool
     */
    function inspire(){
        $page=$this->input('page') ?: 1;
        $page_size=10;
        $data=$this->db
            ->where('users_position.pid',$this->user->id)
            ->where('users_position.lay',2)
            ->where('users.level!=1')
            ->join('users',"users.id=users_position.user_id",'left')
            ->get('users_position',[($page-1)*$page_size,$page_size],'users.headimg,users.nickname,users.mobile,users.createtime');
        if($data){
            foreach ($data as $k=>&$v){
                $v['headimg'] = QINIU_URL.$v['headimg'];
                $v['mobile'] = Checking::replaceStar($v['mobile']);
            }
        }
        return $this->writeJson(200,$data,'成功');
    }
}